package pailaidai.hc.com.mysvgdemo.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Base64;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2018/4/19.
 */

public class CornerLayout2 extends ViewGroup {


    public CornerLayout2(Context context) {
        super(context);
    }

    public CornerLayout2(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CornerLayout2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
        int leftPadding=getPaddingLeft();
        int rightPadding=getPaddingRight();
        int topPadding=getPaddingTop();
        int bottomPadding=getPaddingBottom();


        for(int a=0;a<getChildCount();a++){
            View view=getChildAt(i);
            MarginLayoutParams layoutParams= (MarginLayoutParams) view.getLayoutParams();
            int leftMargin=layoutParams.leftMargin;
            int rightMargin=layoutParams.rightMargin;
            int topMargin=layoutParams.topMargin;
            int bottommargin=layoutParams.bottomMargin;
            if(a==0){
               view.layout(leftMargin+leftPadding,topMargin+topPadding,leftMargin+leftPadding+view.getMeasuredWidth(),topMargin+topPadding+view.getMeasuredHeight());
            }else if(a==1){
               view.layout(getMeasuredWidth()-view.getMeasuredWidth()-leftPadding-leftMargin,topMargin+topPadding,getMeasuredWidth()-rightMargin-rightPadding,view.getMeasuredHeight()+topMargin+topPadding);
            }

        }




    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //先测量多有子组件的大小
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        //测量自己的大小
        int width = measureWidth(widthMeasureSpec);
        int height = measureHeighe(heightMeasureSpec);
        setMeasuredDimension(width, height);
    }

    private int measureHeighe(int heightMeasureSpec) {
        int mode = MeasureSpec.getMode(heightMeasureSpec);
        int size = MeasureSpec.getSize(heightMeasureSpec);
        int height = 0;
        if (mode == MeasureSpec.EXACTLY) {
            height = size;
        } else if (mode == MeasureSpec.AT_MOST) {
            int aHeight, bHeight, cHeight, dHeight;
            int marginHa, marginHb, marginHc, marginHd;
            aHeight = bHeight = cHeight = dHeight = 0;
            marginHa = marginHb = marginHc = marginHd = 0;
            for (int i = 0; i < getChildCount(); i++) {
                MarginLayoutParams layoutParams = (MarginLayoutParams) getChildAt(i).getLayoutParams();
                if (i == 0) {
                    aHeight = getChildAt(i).getMeasuredHeight();
                    marginHa = layoutParams.topMargin + layoutParams.bottomMargin;
                } else if (i == 1) {
                    bHeight = getChildAt(i).getMeasuredHeight();
                    marginHb = layoutParams.topMargin + layoutParams.bottomMargin;
                } else if (i == 2) {
                    cHeight = getChildAt(i).getMeasuredHeight();
                    marginHc = layoutParams.topMargin + layoutParams.bottomMargin;
                } else if (i == 3) {
                    dHeight = getChildAt(i).getMeasuredHeight();
                    marginHd = layoutParams.topMargin + layoutParams.bottomMargin;
                }
            }
            height = Math.max(aHeight, bHeight) + Math.max(cHeight, dHeight) + getPaddingBottom() + getPaddingTop() + Math.max(marginHa, marginHb) + Math.max(marginHc, marginHd);
        }
        return height;
    }

    private int measureWidth(int widthMeasureSpec) {
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        int size = MeasureSpec.getSize(widthMeasureSpec);
        int width = 0;
        if (mode == MeasureSpec.EXACTLY) {
            width = size;
        } else if (mode == MeasureSpec.AT_MOST) {
            int aWidth, bWidth, cWidth, dWidth;
            int margina, marginb, marginc, margind;
            aWidth = bWidth = cWidth = dWidth = 0;
            margina = marginb = marginc = margind = 0;
            for (int i = 0; i < getChildCount(); i++) {
                MarginLayoutParams layoutParams = (MarginLayoutParams) getChildAt(i).getLayoutParams();
                if (i == 0) {
                    aWidth = getChildAt(i).getMeasuredWidth();
                    margina += layoutParams.rightMargin + layoutParams.leftMargin;
                } else if (i == 1) {
                    bWidth = getChildAt(i).getMeasuredWidth();
                    marginb += layoutParams.rightMargin + layoutParams.leftMargin;
                } else if (i == 2) {
                    cWidth = getChildAt(i).getMeasuredWidth();
                    marginc += layoutParams.leftMargin + layoutParams.rightMargin;
                } else if (i == 3) {
                    dWidth = getChildAt(i).getMeasuredWidth();
                    margind += layoutParams.rightMargin + layoutParams.leftMargin;
                }

            }
            width = Math.max(aWidth, cWidth) + Math.max(margina, marginc) + Math.max(bWidth, dWidth) + Math.max(marginb, margind) + getPaddingRight() + getPaddingLeft();
        }
        return width;
    }


    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(this.getContext(), attrs);
    }

    @Override
    protected LayoutParams generateLayoutParams(LayoutParams p) {
        return new MarginLayoutParams(p);
    }

    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        return new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    }
}
